import { defineStore } from 'pinia'
export const useUserStore = defineStore('user', {
	state: () => {
		return {
			name: '谢坤',
			age: '',
			password: ''
		}
	},
	getters: {
		getName: state => {
			return state.name
		}
	},
	actions: {
		SET_NAME(name: string) {
			this.name = name
		}
	}
})

export const useTabsStore = defineStore('tabs', {
	state: () => ({
		tabs: [], // 存储所有标签页
		activeTab: null // 当前活动标签页
	}),
	actions: {
		// 添加新标签页
		addTab(route) {
			const existingTab = this.tabs.find(tab => tab.path === route.path)

			if (!existingTab) {
				this.tabs.push({
					title: route.meta.title || '未命名',
					path: route.path,
					fullPath: route.fullPath,
					query: route.query,
					params: route.params,
					key: Date.now().toString() // 唯一标识
				})
			}

			this.activeTab = existingTab ? existingTab.path : route.path
		},

		// 关闭标签页
		closeTab(path) {
			const index = this.tabs.findIndex(tab => tab.path === path)
			if (index !== -1) {
				this.tabs.splice(index, 1)

				// 如果关闭的是当前活动标签，激活前一个标签
				if (path === this.activeTab) {
					this.activeTab = this.tabs[index - 1]?.path || this.tabs[0]?.path || null
				}
			}
		},

		// 设置活动标签
		setActiveTab(path) {
			this.activeTab = path
		}
	}
})
